<template>
	<view class="wrap safe-area flex column align-center">
		<view class="wrap-white-bg">
			<block v-for="(item, index) in list" :key="index">
				<rowGroup
					:item="item"
					v-model="dataInfo[item.key]"
					v-if="item.type === 'group' && item.direct === 'row' && item.isShow"
				/>

				<columnInput
					:item="item"
					v-model="dataInfo[item.key]"
					v-else-if="item.type === 'input' && item.direct === 'column'"
				/>

				<wrapTextarea :item="item" v-model="dataInfo[item.key]" v-else-if="item.type === 'textarea'" />

				<wrapUpload
					:item="item"
					v-model="dataInfo[item.key]"
					v-else-if="item.type === 'upload' && item.isShow"
				/>

				<wrapFile :item="item" v-model="dataInfo[item.key]" v-else-if="item.type === 'file' && item.isShow" />

				<wrapVideo :item="item" v-model="dataInfo[item.key]" v-else-if="item.type === 'video' && item.isShow" />

				<columnGroup
					:item="item"
					v-model="dataInfo[item.key]"
					v-else-if="item.type === 'group' && item.direct === 'column' && item.isShow"
				/>

				<wrapDistance
					:item="item"
					:area.sync="dataInfo.area"
					:lat.sync="dataInfo.lat"
					:lng.sync="dataInfo.lng"
					v-model="dataInfo[item.key]"
					v-else-if="item.type === 'distance' && item.isShow"
				/>

				<areaList :item="item" v-model="dataInfo[item.key]" v-else-if="item.type === 'area' && item.isShow" />

				<wrapRecommend
					:item="item"
					v-model="dataInfo[item.key]"
					:shopID.sync="dataInfo.shop_id"
					:goodsID.sync="dataInfo.goods_id"
					v-else-if="item.type === 'recommend' && item.isShow"
				/>

				<unitInput
					v-else-if="item.type === 'input' && item.direct === 'unit' && item.isShow"
					:item="item"
					v-model="dataInfo[item.key]"
				/>

				<couponInput
					v-else-if="item.type === 'input' && item.direct === 'coupon' && item.isShow"
					:item="item"
					v-model="dataInfo[item.key]"
					:full.sync="dataInfo.limit_money"
				/>

				<rowChoose
					:item="item"
					v-model="dataInfo[item.key]"
					v-else-if="item.type === 'choose' && item.direct === 'row' && item.isShow"
				/>

				<wrapRedPrice v-else-if="item.type === 'redPrice' && item.isShow" :item="item" />

				<wrapCouponTips v-else-if="item.type === 'coupon' && item.isShow" :item="item" />
			</block>
		</view>

		<view class="sure-btn theme-gradient-btn f-34" @click="onPublish">确认发布</view>
	</view>
</template>

<script>
	import { publishArticle } from "@/API/article.js";
	import rowGroup from "@/components/form/rowGroup.vue";
	import columnInput from "@/components/form/columnInput.vue";
	import wrapTextarea from "@/components/form/textarea.vue";
	import wrapUpload from "@/components/form/upload.vue";
	import wrapVideo from "@/components/form/video.vue";
	import wrapFile from "@/components/form/videoFile.vue";
	import columnGroup from "@/components/form/columnGroup.vue";
	import areaList from "@/components/form/areaList.vue";
	import wrapDistance from "@/components/form/distance.vue";
	import wrapRecommend from "@/components/form/recommend.vue";
	import unitInput from "@/components/form/unitInput.vue";
	import couponInput from "@/components/form/couponLimitInput.vue";
	import rowChoose from "@/components/form/rowChoose.vue";
	import wrapRedPrice from "@/components/form/redPrice.vue";
	import wrapCouponTips from "@/components/form/couponTips.vue";

	export default {
		components: {
			rowGroup,
			columnInput,
			wrapTextarea,
			wrapUpload,
			wrapVideo,
			wrapFile,
			areaList,
			wrapDistance,
			wrapRecommend,
			columnGroup,
			unitInput,
			couponInput,
			rowChoose,
			wrapRedPrice,
			wrapCouponTips,
		},
		data() {
			return {
				dataInfo: {
					article_type: "1",
					type: "1",
					title: "", //标题
					content: "", //内容
					image: "", //封面图片
					video: "", //视频地址
					images: "", //图片
					release_type: "0", //发布位置
					range: "", //发布范围
					area: "", //发布区域
					spread_type: "", //推广类型
					lat: "",
					lng: "",
					goods_id: "", //商品ID
					shop_id: "", //店铺ID
					is_spread: "1", //推广
					bonus_type: "0", //红包类型  0:现金 1:优惠券
					num: "", //数量
					distribution_type: "0", //0=普通红包,1=拼手气红包
					money: "", //现金时为红包金额 优惠券时为减少金额
					limit_money: "", //优惠券的时候 使用条件
					end_time: "", //优惠券到期时间
				},
				isBefore: false,
				beforeInfo: null,
				userInfo: null, //用户信息
			};
		},
		watch: {
			"dataInfo.release_type": {
				handler(newVal, oldVal) {
					if (oldVal !== undefined && (this.beforeInfo === null || this.isBefore)) {
						this.dataInfo.area = "";
						this.dataInfo.lat = "";
						this.dataInfo.lng = "";
					}
				},
				immediate: true,
			},
			"dataInfo.bonus_type": {
				handler(newVal, oldVal) {
					if (oldVal !== undefined && (this.beforeInfo === null || this.isBefore)) {
						this.dataInfo.num = "";
						this.dataInfo.money = "";
						this.dataInfo.end_time = "";
						this.dataInfo.limit_money = "";
					}
				},
				immediate: true,
			},
		},
		computed: {
			list() {
				return [
					{
						title: "发布类型",
						require: true,
						type: "group",
						direct: "row",
						key: "article_type",
						tips: "请选择发布类型",
						list: [
							{ id: "0", title: "图文" },
							{ id: "1", title: "视频" },
						],
						isShow: true,
					},
					{
						title: "标题",
						require: true,
						type: "input",
						key: "title",
						place: "请输入标题",
						mode: "text",
						maxlength: 100,
						tips: "请输入标题",
						direct: "column",
						isShow: true,
					},
					{
						title: "编辑文案",
						require: true,
						type: "textarea",
						key: "content",
						place: "编辑文案~",
						mode: "text",
						maxlength: -1,
						tips: "请输入编辑文案",
						isShow: true,
					},
					{
						title: "图片",
						require: false,
						type: "upload",
						key: "images",
						tips: "请上传图片",
						isShow: this.dataInfo.article_type === "0",
					},
					// {
					// 	title: "上传封面图",
					// 	require: false,
					// 	type: "file",
					// 	key: "image",
					// 	tips: "请上传封面图",
					// 	isShow: this.dataInfo.article_type === "1",
					// },
					{
						title: "上传视频",
						require: true,
						type: "video",
						key: "video",
						tips: "请上传视频",
						isShow: this.dataInfo.article_type === "1",
					},
					{
						title: "选择发布位置",
						require: true,
						type: "group",
						direct: "column",
						key: "release_type",
						tips: "请选择发布位置",
						list: [
							{ id: "0", title: "按距离" },
							{ id: "1", title: "按区域（可多选区域）" },
						],
						isShow: true,
					},
					{
						title: "选择发布距离",
						require: true,
						type: "distance",
						key: "range",
						tips: "请选择发布距离",
						list: [
							{ id: "1", title: "1km" },
							{ id: "2", title: "3km" },
							{ id: "3", title: "5km" },
							{ id: "4", title: "10km" },
						],
						isShow: this.dataInfo.release_type === "0",
					},
					{
						title: "选择发布区域",
						require: true,
						type: "area",
						key: "area",
						tips: "请选择发布区域",
						isShow: this.dataInfo.release_type === "1",
					},
					{
						require: true,
						type: "recommend",
						key: "spread_type",
						tips: "请选择推广信息",
						isShow: this.userInfo === null ? false : this.userInfo.code === 1,
					},
					{
						title: "选择推广类型",
						require: true,
						type: "group",
						direct: "row",
						key: "bonus_type",
						tips: "请选择推广类型",
						list:
							this.userInfo === null
								? [{ id: "0", title: "红包推广" }]
								: this.userInfo.code === 0
								? [{ id: "0", title: "红包推广" }]
								: [
										{ id: "0", title: "红包推广" },
										{ id: "1", title: "优惠券推广" },
								  ],
						isShow: true,
					},
					{
						require: true,
						type: "input",
						direct: "unit",
						key: "money",
						place: "请输入红包总金额",
						tips: "请输入红包总金额",
						mode: "digit",
						maxlength: "6",
						unit: "元",
						isShow: this.dataInfo.bonus_type === "0",
					},
					{
						require: true,
						type: "input",
						direct: "unit",
						key: "num",
						place: "请输入红包总数量",
						tips: "请输入红包总数量",
						mode: "number",
						maxlength: "5",
						unit: "份",
						isShow: this.dataInfo.bonus_type === "0",
					},
					{
						title: "红包类型",
						require: true,
						type: "group",
						direct: "row",
						key: "distribution_type",
						tips: "请选择红包类型",
						list: [
							{ id: "0", title: "普通钱包" },
							{ id: "1", title: "拼手气红包" },
						],
						isShow: this.dataInfo.bonus_type === "0",
					},
					{
						type: "redPrice",
						title: "应付金额：",
						price: this.dataInfo.money === "" ? "0.00" : this.dataInfo.money,
						isShow: this.dataInfo.bonus_type === "0",
					},
					{
						require: true,
						type: "input",
						direct: "unit",
						key: "num",
						place: "请输入优惠券总数量",
						tips: "请输入优惠券总数量",
						mode: "number",
						maxlength: "6",
						unit: "份",
						isShow: this.dataInfo.bonus_type === "1",
					},
					{
						require: true,
						type: "input",
						direct: "coupon",
						key: "money",
						place: "请输入减少的金额",
						tips: "请输入减少的金额",
						mode: "digit",
						maxlength: "6",
						isShow: this.dataInfo.bonus_type === "1",
					},
					{
						title: "优惠券到期时间",
						require: true,
						type: "choose",
						key: "end_time",
						mode: "date",
						place: "请选择到期时间",
						direct: "row",
						list: [],
						tips: "请选择到期时间",
						isShow: this.dataInfo.bonus_type === "1",
					},
					{
						type: "coupon",
						title: "使用条件填写0为无门槛优惠券",
						isShow: this.dataInfo.bonus_type === "1",
					},
				];
			},
		},
		onLoad() {
			this.$commonNetwork.getUserIdenInfoData((res) => {
				this.userInfo = res;
			});
		},
		methods: {
			/**
			 * 发布
			 */
			onPublish() {
				if (this.$filters.judgeFormData(this.list, this.dataInfo)) {
					if (this.dataInfo.release_type === "0" && this.dataInfo.area === "") {
						this.$st.show("请选择发布位置的中心点");
						return;
					}

					if (this.dataInfo.spread_type === "0" && this.dataInfo.goods_id === "") {
						this.$st.show("请选择推广的商品");
						return;
					}
					if (this.dataInfo.spread_type === "2" && this.dataInfo.goods_id === "") {
						this.$st.show("请选择推广的套餐/卡券");
						return;
					}
					if (this.dataInfo.spread_type === "3" && this.dataInfo.goods_id === "") {
						this.$st.show("请选择自提商品");
						return;
					}
					if (this.dataInfo.spread_type === "4" && this.dataInfo.goods_id === "") {
						this.$st.show("请选择同城配送商品");
						return;
					}

					if (this.dataInfo.bonus_type === "1" && this.dataInfo.limit_money === "") {
						this.$st.show("请输入使用条件");
						return;
					}

					let obj = { ...this.dataInfo };
					obj.article_category_id = "0";
					this.$throttle(() => {
						publishArticle(obj).then((res) => {
							if (res.data.token === undefined) {
								this.$st.success("发布成功", () => {
									this.$jump.paySuccess("coupon", `&article_id=${res.data.article_id}`);
								});
							} else {
								uni.navigateTo({
									url:
										"/pages/user/money/pay?token=" +
										res.data.token +
										"&data=" +
										JSON.stringify(res.data.list) +
										"&article_id=" +
										res.data.article_id,
								});
							}
						});
					});
				}
			},
		},
	};
</script>

<style>
	@import "@/css/publish.css";
</style>
